import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @author lys
 * 查看借阅记录
 */
public class CheckBorrowRecords {
    private static CheckBorrowRecords instance;
    public JPanel jPanel = new JPanel();
    public JLayeredPane jLayeredPane = new JLayeredPane();

    private JLabel jLabel = new JLabel("借阅记录");
    private JLabel jLabel2 = new JLabel("请输入书号:");
    private JTextField field = new JTextField(25);

    private Dimension dimension = new Dimension(220, 30);
    public DefaultTableModel model = new DefaultTableModel();

    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private Font font2 = new Font("宋体", Font.BOLD, 20);

    private JButton button = new JButton("搜索");
    private CheckBorrowRecords() {
        Icon i = new ImageIcon(CheckBorrowRecords.class.getResource("img/13.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        jLabel.setFont(font);
        jLabel.setBounds(485, 35, 800, 100);
        jLabel.setForeground(Color.white);
        jLabel2.setFont(font1);
        jLabel2.setBounds(180, 172, 250, 30);
        jLabel2.setForeground(Color.white);

        // 文本框
        field.setFont(font2);
        field.setSize(dimension);
        field.setBackground(Color.cyan);
        field.setBounds(480, 173, 250, 35);
        field.setForeground(Color.white);
        field.setOpaque(false);

        // 按钮
        button.setFont(font1);
        button.setBounds(850, 170, 100, 40);
        button.setForeground(Color.white);
        button.setBackground(Color.white);
        button.setOpaque(false);


        // 表格
        model.addColumn("书号", new Vector<Integer>());
        model.addColumn("书名", new Vector<Integer>());
        model.addColumn("借书人", new Vector<Integer>());
        model.addColumn("借书日期", new Vector<Integer>());
        model.addColumn("是否归还", new Vector<Integer>());
        model.addColumn("还书日期", new Vector<Integer>());
        JTable jTable = new JTable(model);
        jTable.setOpaque(false);

        JScrollPane pane = new JScrollPane(jTable);
        pane.setBounds(200, 250, 800, 400);
        pane.setOpaque(false);

        jTable.setVisible(false);
        pane.setVisible(false);

//		for(int k = 0; k < 30; k++) {
//			model.addRow(new Vector<Integer>());
//		}
        // FindBook.allbook(model);

        JTableHeader head = jTable.getTableHeader();
        // 设置表头的大小
        head.setPreferredSize(new Dimension(head.getWidth(), 30));
        // 设置表头字体大小
        head.setFont(new Font("宋体", Font.BOLD, 20));
        // head.setForeground(Color.cyan);
        head.setBackground(Color.cyan);
        // 设置表格的行宽
        jTable.setRowHeight(30);
        // 设置表格行中字体大小
        jTable.setFont(new Font("宋体", Font.ROMAN_BASELINE, 17));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        jTable.setDefaultRenderer(Object.class, renderer);

        jLayeredPane.add(jLabel,new Integer(100),1);
        jLayeredPane.add(jLabel2,new Integer(100),2);
        jLayeredPane.add(field,new Integer(100),3);
        jLayeredPane.add(button,new Integer(100),4);
        jLayeredPane.add(pane,new Integer(100),5);
        jLayeredPane.add(Label,new Integer(0),0);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTable.setVisible(true);
                pane.setVisible(true);

                ArrayList<BorrowInfo> borrowRecords;
                DefaultTableModel model2 = (DefaultTableModel) jTable.getModel();
                model2.getDataVector().clear();
                model2.fireTableDataChanged();
                jTable.updateUI();
                if(field.getText().length() == 0){
                    borrowRecords = returnAllBorrowRecords();
                }else {
                    borrowRecords = returnSpecificBorrowRecords(field.getText());
                }
                for(BorrowInfo borrowInfo: borrowRecords){
                    String[] column = {String.valueOf(borrowInfo.getBookId()), borrowInfo.getBookName(), borrowInfo.getBorrowUser(), String.valueOf(borrowInfo.getBorrowTime()), "", ""};
                    if(borrowInfo.getReturned()){
                        column[4] = "已还";
                    }
                    else{
                        column[4] = "未还";
                    }
                    if(borrowInfo.getReturnTime() != null){
                        column[5] = String.valueOf(borrowInfo.getReturnTime());
                    }
                    model2.addRow(column);
                }
            }
        });

        button.setFocusPainted(false);
    }

    public static CheckBorrowRecords getInstance() {
        if (instance==null) instance = new CheckBorrowRecords();
        return instance;
    }
    /**
     * @author zj
     * @return 返回所有用户借阅信息
     */
    public ArrayList<BorrowInfo> returnAllBorrowRecords(){
        ResultSet rs = null;
        PreparedStatement preSql;
        String sql;
        Connection con = DatabaseConnection.getInstance().getCon();
        sql = "select * from borrowinfo";
        ArrayList<BorrowInfo> borrowRecords = new ArrayList<>();
        try{
            preSql = con.prepareStatement(sql);
            rs = preSql.executeQuery();
            while(rs.next()){
                int bookId = rs.getInt("bookid");
                String bookName = rs.getString("bookname");
                String borrowUser = rs.getString("borrowuser");
                Date borrowTime = rs.getDate("borrowtime");
                Boolean isReturned = rs.getBoolean("isreturned");
                Date returnTime = rs.getDate("returntime");
                BorrowInfo borrowInfo = new BorrowInfo(bookId, bookName, borrowUser, borrowTime, isReturned, returnTime);
                borrowRecords.add(borrowInfo);
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return borrowRecords;
    }

    /**
     *
     * @param bookId
     * @return 返回特定书籍的借阅信息
     */
    public ArrayList<BorrowInfo> returnSpecificBorrowRecords(String bookId){
        ResultSet rs = null;
        PreparedStatement preSql;
        String sql;
        Connection con = DatabaseConnection.getInstance().getCon();
        sql = "select * from borrowinfo where bookid = ?";
        ArrayList<BorrowInfo> borrowRecords = new ArrayList<>();
        try{
            preSql = con.prepareStatement(sql);
            preSql.setString(1, bookId);
            rs = preSql.executeQuery();
            while(rs.next()){
                int bookid = rs.getInt("bookid");
                String bookName = rs.getString("bookname");
                String borrowUser = rs.getString("borrowuser");
                Date borrowTime = rs.getDate("borrowtime");
                Boolean isReturned = rs.getBoolean("isreturned");
                Date returnTime = rs.getDate("returntime");
                BorrowInfo borrowInfo = new BorrowInfo(bookid, bookName, borrowUser, borrowTime, isReturned, returnTime);
                borrowRecords.add(borrowInfo);
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return borrowRecords;
    }
    public static void removeInstance(){
        instance = null;
    }
}
